package mainmodel

import (
	"sync"
	"web_server/model"
)

var roleMap map[int64]string = nil
var roleMapMut sync.RWMutex

func GetRoleMap() map[int64]string {
	roleMapMut.RLock()
	if roleMap != nil {
		roleMapMut.RUnlock()
		return roleMap
	}
	roleMapMut.RUnlock()
	roleMapMut.Lock()
	defer roleMapMut.Unlock()
	if roleMap != nil {
		return roleMap
	}
	roleMap = make(map[int64]string)
	var roleList []struct {
		Id   int64
		Name string
	}
	if err := model.DBSlaveRand().Select(&roleList, "SELECT id, name FROM t_base_role"); err != nil {
		panic(err)
	}
	for _, v := range roleList {
		roleMap[v.Id] = v.Name
	}
	return roleMap
}

func ClearRoleMap() {
	roleMapMut.Lock()
	defer roleMapMut.Unlock()
	roleMap = nil
}
